﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLThuChi.Repositories;
using QLThuChi.Helpers;

namespace QLThuChi.OtherForms
{
    public partial class FrmChonCongTrinhGiaiDoan : DevComponents.DotNetBar.Office2007Form
    {
        private int _selectedGiaiDoan;
        // tuansl added: event handler to transfer data to other forms
        public EventHandler Handler { get; set; }

        public FrmChonCongTrinhGiaiDoan()
        {
            InitializeComponent();
        }

        private void FrmChonGiaiDoan_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        public void LoadData()
        {
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode("Danh mục các công trình");
            root.ImageIndex = 0;
            treeView1.Nodes.Add(root);

            try
            {
                // Load list all of cong trinh
                var lstCongTrinh = CongTrinhRepository.SelectAll();
                for (int i = 0; i < lstCongTrinh.Count; i++)
                {
                    var congtrinh = lstCongTrinh[i];
                    TreeNode congtrinhnode = new TreeNode(congtrinh.MaCongTrinh + " - " + congtrinh.TenCongTrinh);
                    congtrinhnode.ImageIndex = 1;
                    root.Nodes.Add(congtrinhnode);

                    // Load giai doan corresponding with specified cong trinh
                    var lstGiaiDoan = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(congtrinh.MaCongTrinh);
                    for (int j = 0; j < lstGiaiDoan.Count; j++)
                    {
                        var giaidoan = lstGiaiDoan[j];
                        TreeNode giaidoannode = new TreeNode(giaidoan.MaGiaiDoan + " - " + giaidoan.GiaiDoan.TenGiaiDoan);
                        giaidoannode.ImageIndex = 2;
                        congtrinhnode.Nodes.Add(giaidoannode);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        private void btChon_Click(object sender, EventArgs e)
        {
            var giaidoan = CongTrinh_GiaiDoanRepository.SelectByID(_selectedGiaiDoan);
            TransferDataInfo(this, new MyEvent(_selectedGiaiDoan.ToString()));
        }

        public int GetLevelTreeView(TreeNode node)
        {
            if (node.Parent == null)
                return 1;
            else if (node.Parent.Parent == null)
                return 2;
            else if (node.Parent.Parent.Parent == null)
                return 3;
            else if (node.Parent.Parent.Parent.Parent == null)
                return 4;
            return -1;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int level = GetLevelTreeView(treeView1.SelectedNode);
            if (level == 3)
            {
                btChon.Enabled = true;
                string magiaidoan = treeView1.SelectedNode.Text.Split('-')[0].Trim();
                string macongtrinh = treeView1.SelectedNode.Parent.Text.Split('-')[0].Trim();
                var giaidoan = CongTrinh_GiaiDoanRepository.SelectByCongTrinhGiaiDoan(int.Parse(macongtrinh), int.Parse(magiaidoan));
                if (giaidoan != null)
                {
                    _selectedGiaiDoan = giaidoan.MaCongTrinhGiaiDoan;
                }
            }
            else
            {
                btChon.Enabled = false;
            }
        }

        /// <summary>
        /// tuansl added: function is used to transfer data when event would be raised
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void TransferDataInfo(object sender, MyEvent e)
        {
            //this.Close();
            this.Handler(this, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmChonGiaiDoan_Shown(object sender, EventArgs e)
        {
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            int level = GetLevelTreeView(treeView1.SelectedNode);
            if (level == 3)
            {
                var giaidoan = CongTrinh_GiaiDoanRepository.SelectByID(_selectedGiaiDoan);
                TransferDataInfo(this, new MyEvent(_selectedGiaiDoan.ToString()));
            }
        }
    }
}